package cn.iocoder.yudao.module.member.controller.admin.jobView;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.jobView.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.jobView.JobViewDO;
import cn.iocoder.yudao.module.member.convert.jobView.JobViewConvert;
import cn.iocoder.yudao.module.member.service.jobView.JobViewService;

@Api(tags = "管理后台 - 职位浏览记录")
@RestController
@RequestMapping("/member/job-view")
@Validated
public class JobViewController {

    @Resource
    private JobViewService jobViewService;

    @PostMapping("/create")
    @ApiOperation("创建职位浏览记录")
    @PreAuthorize("@ss.hasPermission('member:job-view:create')")
    public CommonResult<Long> createJobView(@Valid @RequestBody JobViewCreateReqVO createReqVO) {
        return success(jobViewService.createJobView(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新职位浏览记录")
    @PreAuthorize("@ss.hasPermission('member:job-view:update')")
    public CommonResult<Boolean> updateJobView(@Valid @RequestBody JobViewUpdateReqVO updateReqVO) {
        jobViewService.updateJobView(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除职位浏览记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('member:job-view:delete')")
    public CommonResult<Boolean> deleteJobView(@RequestParam("id") Long id) {
        jobViewService.deleteJobView(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得职位浏览记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('member:job-view:query')")
    public CommonResult<JobViewRespVO> getJobView(@RequestParam("id") Long id) {
        JobViewDO jobView = jobViewService.getJobView(id);
        return success(JobViewConvert.INSTANCE.convert(jobView));
    }

    @GetMapping("/list")
    @ApiOperation("获得职位浏览记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('member:job-view:query')")
    public CommonResult<List<JobViewRespVO>> getJobViewList(@RequestParam("ids") Collection<Long> ids) {
        List<JobViewDO> list = jobViewService.getJobViewList(ids);
        return success(JobViewConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得职位浏览记录分页")
    @PreAuthorize("@ss.hasPermission('member:job-view:query')")
    public CommonResult<PageResult<JobViewRespVO>> getJobViewPage(@Valid JobViewPageReqVO pageVO) {
        PageResult<JobViewDO> pageResult = jobViewService.getJobViewPage(pageVO);
        return success(JobViewConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出职位浏览记录 Excel")
    @PreAuthorize("@ss.hasPermission('member:job-view:export')")
    @OperateLog(type = EXPORT)
    public void exportJobViewExcel(@Valid JobViewExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<JobViewDO> list = jobViewService.getJobViewList(exportReqVO);
        // 导出 Excel
        List<JobViewExcelVO> datas = JobViewConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "职位浏览记录.xls", "数据", JobViewExcelVO.class, datas);
    }

}
